package com.msa.msaportal.webservice;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.json.JSONException;
import org.json.JSONObject;

import com.msa.msaportal.entity.Portalfooter;
import com.msa.msaportal.entity.Template;
import com.msa.msaportal.service.IFooterService;
import com.msa.msaportal.service.ITemplateService;

public class FooterServicesImpl implements FooterServices {
	@Resource(name = "footerService")
	private IFooterService footerService;
	@Resource(name = "templateManager")
	private ITemplateService templateManager;

	String success = "{'result':'0','info':'操作成功!'}";
	String fail = "{'result':'1','info':'操作失败!'}";
	String exception = "{'result':'2','info':'接口异常!'}";

	@Override
	public String addFooter(String data) throws Exception {
		try {
			Portalfooter footer = new Portalfooter();
			JSONObject dataJson = new JSONObject(data);
			footer.setIspublic(Integer.valueOf(dataJson.getString("public")));
			footer.setOrgname(dataJson.getString("orgName"));
			footer.setCreatetime(dataJson.getString("createTime"));
			footer.setUpdatetime(dataJson.getString("updateTime"));
			footer.setUpdateusername(dataJson.getString("alter"));
			footer.setCreateusername(dataJson.getString("author"));
			String orgId = dataJson.getString("orgId");
			if (orgId.trim().equals("")) {
				return "{'result':'4','info':'orgId不能为空!'}";
			}
			footer.setOrgid(orgId);
			footer.setCreateuserid(dataJson.getString("authorId"));
			footer.setUpdateuserid(dataJson.getString("alterId"));
			footer.setFooter_content(dataJson.getString("footContent"));
			// 获得footerId
			String footerId = dataJson.getString("FooterId");
			footer.setFsource_id(footerId);
			// 获得模板id
			String templateId = dataJson.getString("templateOrStyleId");

			try {
				// 保存footer对象
				footerService.addFooter(footer);
				// 获得footer
				Portalfooter portalfooter = footerService.find(footerId);
				// 通过模板Id查询模板并保存portalfooter到模板
				Template template = templateManager.findByTresourse_Id(templateId);
				template.setFooter(portalfooter);
				templateManager.addHeadOrFoontToTmp(template);
			} catch (Exception e) {
				e.printStackTrace();
				return fail;
			}
		} catch (Exception e) {
			// TODO: handle exception
			return exception;
		}
		return success;
	}

	@Override
	public String changeFooter(String data) {
		try {

			JSONObject dataJson = new JSONObject(data);
			String footerId = dataJson.getString("footerId");
			String footerContent = dataJson.getString("footerContent");
			String isPublic = dataJson.getString("public");
			String orgId=dataJson.getString("orgId");
			try {
				// 查询/设置内容
				Portalfooter portalfooter = footerService.find(footerId);
				if(portalfooter==null){
					portalfooter=new Portalfooter();
					portalfooter.setFsource_id(footerId);
					footerService.addFooter(portalfooter);
					Portalfooter portalfooter2=footerService.find(footerId);
					System.out.println("portalfooter2"+portalfooter2);
					Template template = templateManager.findByTresourse_Id(footerId);
					template.setFooter(portalfooter2);
					System.out.println(template);
					templateManager.addHeadOrFoontToTmp(template);
				}
				portalfooter.setFsource_id(footerId);
				portalfooter.setIspublic(Integer.parseInt(isPublic));
				portalfooter.setOrgid(orgId);
				portalfooter.setFooter_content(footerContent);

				// 更新内容
				footerService.update(portalfooter);

			} catch (Exception e) {
				e.printStackTrace();
				return "{'result':'1','info':'操作失败!'}";
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "{'result':'2','info':'接口异常'}";
		}
		return "{'result':'0','info':'操作成功!'}";
	}

}
